package com.smile.stream.group;

import com.smile.stream.StreamUtil;
import com.smile.stream.pojo.Dish;
import com.smile.stream.pojo.Type;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 21:21
 */
public class GroupDemo1 {
    public static void main(String[] args) {
        List<Dish> menu = StreamUtil
                .getMenu();
        Map<Type, List<Dish>> collect = menu
                .stream()
                .collect(Collectors.groupingBy(Dish::getType));
        collect.entrySet().forEach(System.out::println);
        /**
         * 输出结果
         * FISH=[prawns, salmon]
         * MEAT=[pork, beef, chicken]
         * OTHER=[french fries, rice, season fruit, pizza]
         */

        Map<CaloricLevel, List<Dish>> dishesByCaloricLevel = menu
                .stream()
                .collect(Collectors.groupingBy(dish -> {
                        if (dish.getCalories() <= 400) {
                            return CaloricLevel.DIET;
                        } else if (dish.getCalories() <= 700) {
                            return CaloricLevel.NORMAL;
                        } else {
                            return CaloricLevel.FAT;
                        }
                 }));
        dishesByCaloricLevel.entrySet().forEach(System.out::println);
        /**
         * 输出结果
         * NORMAL=[beef, french fries, pizza, salmon]
         * DIET=[chicken, rice, season fruit, prawns]
         * FAT=[pork]
         */

    }


    public enum CaloricLevel {DIET,NORMAL,FAT}
}
